package com.ruoyi.system.service.Registry.Impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.RegistrationRequest;
import com.ruoyi.system.mapper.Registry.RegistryRequestMapper;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.Registry.IRegisrtryRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class RegistryRequestServiceImpl implements IRegisrtryRequestService {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private RegistryRequestMapper registryRequestMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public RegistrationRequest selectUserRegistrationRequestsById(Long id)
    {
        return registryRequestMapper.selectUserRegistrationRequestsById(id);
    }
    @Override
    public String submitRequest(RegistrationRequest registrationRequest) {
        String msg = "", username = registrationRequest.getUserName(), password = registrationRequest.getPassword();
        String email=registrationRequest.getEmail(),phonenumber=registrationRequest.getPhonenumber(),sex=registrationRequest.getSex(),role=registrationRequest.getRoleName();
        SysUser sysUser = new SysUser();
        sysUser.setUserName(username);
        sysUser.setEmail(email);
        sysUser.setPhonenumber(phonenumber);
//        sysUser.setSex(sex);
//        sysUser.setRoleId(2L);
        registrationRequest.setRequestStatus("PENDING");

        if (StringUtils.isEmpty(username))
        {
            msg = "用户名不能为空";
        }
        else if (StringUtils.isEmpty(password))
        {
            msg = "用户密码不能为空";
        }
        else if (username.length() < UserConstants.USERNAME_MIN_LENGTH
                || username.length() > UserConstants.USERNAME_MAX_LENGTH)
        {
            msg = "账户长度必须在2到20个字符之间";
        }
        else if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
        {
            msg = "密码长度必须在5到20个字符之间";
        }
        else if (!userService.checkUserNameUnique(sysUser))
        {
            msg = "保存用户'" + username + "'失败，注册账号已存在";
        }
        else if (!userService.checkEmailUnique(sysUser)){
            msg = "保存用户'" + username + "'失败，注册邮箱已存在";
        }
        else if (!userService.checkPhoneUnique(sysUser)){
            msg = "保存用户'" + username + "'失败，注册电话已存在";
        }
        else
        {
            msg = "注册申请已提交,请等待审核";
            registryRequestMapper.insertRegistrationRequest(registrationRequest);
//                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.register.success")));
        }
        return msg;

    }

    @Override
    public String approveRequest(RegistrationRequest registrationRequest) {
        String msg = "";

//        String requestStatus= "APPROVED";
        registryRequestMapper.updateRequestStatus(registrationRequest);
        return msg;
    }

    @Override
    public String rejectRequest(RegistrationRequest registrationRequest) {

//        String requestStatus= "REJECTED";
        String msg = "";
        registryRequestMapper.updateRequestStatus(registrationRequest);
        return msg;
    }

    @Override
    public List<RegistrationRequest> selectRegistrationRequestList(RegistrationRequest registrationRequest) {

        //获取登录的用户拥有的角色
        SysUser user= SecurityUtils.getLoginUser().getUser();
        List<SysRole> roles=user.getRoles();
        registrationRequest.setRequestStatus("PENDING");
        //如果是管理员，则显示所有请求
        if(user.isAdmin()){
//            registrationRequest.setRequestStatus("PENDING");
            return registryRequestMapper.selectRegistrationRequestsList(registrationRequest);
        }
        else if(judgeRole(roles)==1){
            //展示TC/SC的请求
            registrationRequest.setRoleName("TC/SC");
            return registryRequestMapper.selectRegistrationRequestsList(registrationRequest);
        }
        else if(judgeRole(roles)==2){
            //展示TC/SC的请求
            registrationRequest.setRoleName("起草单位");
            List<RegistrationRequest> registrationRequestsList= registryRequestMapper.selectRegistrationRequestsList(registrationRequest);
            registrationRequest.setRoleName("专家");
            registrationRequestsList.addAll(registryRequestMapper.selectRegistrationRequestsList(registrationRequest));
            return registrationRequestsList;
        }
        else if(judgeRole(roles)==3){
            //展示专家的请求
            registrationRequest.setRoleName("专家");
            return registryRequestMapper.selectRegistrationRequestsList(registrationRequest);
        }

        else {
            return null;
        }
    }
    int judgeRole(List<SysRole> roles){
        int flag=0;
        for(SysRole role : roles){
            if(role.getRoleName().equals("国标委")|| role.getRoleName().equals("SC")){
                flag=1;
                break;
            }
            else if(role.getRoleName().equals("TC/SC")){
                flag=2;
                break;
            }
            else if(role.getRoleName().equals("专家")){
                flag=3;break;

            }
            else if(role.getRoleName().equals("起草单位")){
                flag=4;break;
            }
        }
        return flag;
    }
}
